package cn.tworice.ocr;

import cn.tworice.ocr.tess4j.OCRClient;
import net.sourceforge.tess4j.TesseractException;
import org.apache.commons.codec.binary.Base64;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class OCRApplication {
    public static void main(String[] args) throws IOException, TesseractException {

        OCRClient ocrClient = new OCRClient();
        System.out.println(ocrClient.ocr("/5.png"));

//        HashMap<String,Object> map = new HashMap<>();
//        String imgStr = "data:image/jpg;base64,"+getImgStr("E:\\work\\tworice-cloud\\tworice-ocr\\src\\main\\resources\\1.jpg");
//        map.put("apikey","K83009953188957");
//        //map.put("url","https://gitee.com/Jarvis_D/typora-images/raw/master/img/0f08de942bb82ff8675f790b9b0a74a.png");
//        map.put("base64Image",imgStr);
//        //map.put("language","chs");
//        map.put("filetype","JPG");
//        map.put("OCREngine",2);
//
//        //map.put("scale",true);
//        String yy = HttpUtil.post("https://api.ocr.space/parse/image",map);
//        //String yy = HttpUtils.doPostToJson("https://api.ocr.space/parse/image", JSON.toJSONString(map));
////        JsonRootBean jsonRootBean = JSON.parseObject(yy, JsonRootBean.class);
////        System.err.println(jsonRootBean.getParsedResults().get(0).getParsedText());
//        System.out.println(yy);
    }

    /**
     * 将图片转换成Base64编码
     * @param imgFile 待处理图片
     * @return
     */
    public static String getImgStr(String imgFile) {
        InputStream in = null;
        byte[] data = null;
        try {
            // 将图片文件转化为字节数组字符串，并对其进行Base64编码处理
            in = new FileInputStream(imgFile);
            data = new byte[in.available()];
            in.read(data);
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return Base64.encodeBase64String(data);
    }

//    public static void tess4j() throws IOException {
//        OCRClient.test();
//    }
}
